const express = require("express");
const app = express();
let apiRoutes = express.Router();

let data = require('./public/data');
let hotDrugs = data.hotDrugs;
let categories = data.categories;
let drugs = data.drugs;
app.use('/api', apiRoutes);

module.exports = {
    publicPath: '/client',
    pages: {
        client: {
            entry: 'src/page/client/main.js',
            template: 'public/client.html',
            filename: 'client'
        },
        server: {
            entry: 'src/page/server/main.js',
            template: 'public/server.html',
            filename: 'server'
        }
    },
    devServer: {
        proxy: 'http://localhost:8081',
        before(app) {
            app.get("/api/hotDrugs", (req, res) => {
                res.json({
                    data: hotDrugs
                });
            });
            app.get("/api/categories", (req, res) => {
                res.json({
                    data: categories
                })
            });
            app.get("/api/drugs", (req, res) => {
                res.json({
                    data: drugs
                })
            });
            app.get("/api/search", (req, res) => {
                if (req.query.searchName === '') {
                    res.json({
                        data: []
                    })
                } else {
                    res.json({
                        data: drugs
                    })
                }
            })
        }
    }
};
